package seeders

import (
	"goravel/app/models"

	"github.com/goravel/framework/facades"
)

type RoleSeeder struct {
}

// Signature The name and signature of the seeder.
func (s *RoleSeeder) Signature() string {
	return "RoleSeeder"
}

// Run executes the seeder logic.
func (s *RoleSeeder) Run() error {
	roles := []map[string]any{
		{
			"role_name":   "超级管理员",
			"description": "拥有所有权限",
		},
		{
			"role_name":   "普通用户",
			"description": "基础权限",
		},
	}

	for _, role := range roles {
		var exists bool
		if err := facades.Orm().Query().Model(&models.Role{}).Where("role_name", role["role_name"].(string)).Exists(&exists); err != nil {
			facades.Log().Error("Failed to check role existence: ", err)
			return err
		}

		if !exists {
			roleModel := models.Role{
				RoleName:    role["role_name"].(string),
				Description: role["description"].(string),
			}

			if err := facades.Orm().Query().Create(&roleModel); err != nil {
				facades.Log().Error("Failed to create role: ", err)
				return err
			}
		}
	}

	return nil
}
